<?php
session_start();
function yzm($width=100,$height=40,$type=1,$fontsize=20,$leng=4,$lineheight=30){
    $img = imagecreatetruecolor($width,$height);
    imagefill($img,0,0,
    imagecolorallocate($img,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255))
    );
    for($i=0;$i<200;$i++){
        imagesetpixel(
            $img,
            mt_rand(0,$width),
            mt_rand(0,$height),
            imagecolorallocate($img,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120))
        );
    }
    switch($type){
    case 1:
            if($leng>10){
            $leng = 10;
            }
            $str ='1234567890';
            break;
    case 2:
            if($leng>26){
                $leng =26;
            }
            $str ='qwertyuipasdfghjkzxcvbnm';
            break;
    case 3:
            if($leng>26){
                $leng =26;
            }
            $str = 'QWERTYUPASDFGHJKLZXCVBNM';
            break;
    case 4:
             if($leng>62){
                $leng =62;
            }
            $str='23456789qwertyuipasdfghjkzxcvbnmQWERTYUPASDFGHJKZXCVBNM';

            break;
    }
    $str = str_shuffle($str);
    $str = substr($str,0,$leng);
    $_SESSION['vcode']=$str;
    // $session = Yii::$app->session;
    // $session->set('vcode' , $str);

    $w = $width/$leng;
    for($i=0;$i<$leng;$i++){
        $x = $i*$w+5;
        $y = mt_rand($fontsize,$lineheight);
        imagettftext($img,$fontsize,mt_rand(-40,40),$x,$y,
         imagecolorallocate($img,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)),'./fonts/4.ttf',$str{$i}
        );    
    }
    header('content-type:image/jpeg');
    imagejpeg($img);
    imagedestroy($img);
    // echo $str;exit;

}
yzm(110,50,4,20,5,30);


